package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.enums.ApplyStatusEnum;
import tech.horgee.group.UpdateGroup;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * 报销表
 *
 * @author haojun
 * @since 2023-12-13
 */
@Getter
@Setter
@TableName("tab_expense")
public class Expense implements Serializable, IWrapper<Expense> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @NotNull(groups = UpdateGroup.class)
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 报销金额
     */
    private BigDecimal amount;

    /**
     * 申请状态
     */
    private ApplyStatusEnum status;

    /**
     * 费用说明
     */
    private String remark;

    /**
     * 附件
     */
    private String annex;

    /**
     * 消费日期
     */
    private LocalDate expenseTime;

    /**
     * 创建时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    @TableField(exist = false)
    private String nickname;

    /**
     * 开始时间
     */
    @TableField(exist = false)
    private LocalDateTime startTime;
    /**
     * 结束时间
     */
    @TableField(exist = false)
    private LocalDateTime endTime;

    public String getStatusLabel() {
        return Optional.ofNullable(status).orElse(ApplyStatusEnum.PROCESSING).label;
    }

    @Override
    public LambdaQueryWrapper<Expense> wrapper() {
        return new LambdaQueryWrapper<Expense>()
                .eq(Objects.nonNull(id), Expense::getId, id)
                .eq(Objects.nonNull(userId), Expense::getUserId, userId)
                .eq(Objects.nonNull(amount), Expense::getAmount, amount)
                .eq(Objects.nonNull(status), Expense::getStatus, status)
                .like(Objects.nonNull(remark), Expense::getRemark, remark)
                .like(Objects.nonNull(annex), Expense::getAnnex, annex)
                .eq(Objects.nonNull(expenseTime), Expense::getExpenseTime, expenseTime)
                .eq(Objects.nonNull(createTime), Expense::getCreateTime, createTime)
                .eq(Objects.nonNull(updateTime), Expense::getUpdateTime, updateTime)
                .ge(Objects.nonNull(startTime), Expense::getExpenseTime, startTime)
                .le(Objects.nonNull(endTime), Expense::getExpenseTime, endTime)
                ;
    }
}
